﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Marbles.Core.Content.Themes;
using System.IO;

namespace Marbles.Web.Admin.Content
{
    public partial class Themes : ContentBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack) { LoadData(); }
        }

        private void LoadData()
        {
            ThemeCollection items = new ThemeCollection();
            cDrpTheme.Items.Clear();
            foreach (Theme t in items)
            {
                cDrpTheme.Items.Add(new ListItem(t.Name, t.Name));
            }

            LoadTheme(cDrpTheme.SelectedValue);
        }

        private void LoadTheme(string name)
        {
            ThemeCollection items = new ThemeCollection();
            foreach (Theme t in items)
            {
                if (t.Name == name)
                {
                    cLstFiles.DataSource = t.Files;
                    cLstFiles.DataBind();
                }
            }
        }

        protected void cDrpTheme_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadTheme(cDrpTheme.SelectedValue);
        }

        protected void Page_Command(object sender, CommandEventArgs e)
        {
            string filename = Server.MapPath("~/App_Themes/" + this.cDrpTheme.SelectedValue + "/" + e.CommandArgument.ToString());
            System.IO.FileInfo info = new System.IO.FileInfo(filename);

            if (!((info.Extension.Equals(".css")) || (info.Extension.Equals(".js")))) { return; }

            switch (e.CommandName)
            {
                case "EditItem":
                    cBtnSave.CommandArgument = e.CommandArgument.ToString();
                    cBtnClose.CommandArgument = e.CommandArgument.ToString();
                    cTxtEdit.Text = "";
                    if (File.Exists(filename))
                    {
                        StreamReader sr = new StreamReader(filename);
                        cTxtEdit.Text = sr.ReadToEnd().Trim();
                        sr.Close();
                    }
                    cDlgEdit.Visible = true;
                    break;
                case "Save":
                    if (File.Exists(filename)) { File.Delete(filename); }
                    StreamWriter sw = new StreamWriter(filename);
                    sw.Write(cTxtEdit.Text.Trim());
                    sw.Flush();
                    sw.Close();
                    cTxtEdit.Text = "";
                    LoadTheme(cDrpTheme.SelectedValue);
                    cDlgEdit.Visible = false;
                    break;
                case "Close":
                    cBtnClose.CommandArgument = string.Empty;
                    cTxtEdit.Text = "";
                    cBtnSave.CommandArgument = string.Empty;
                    cDlgEdit.Visible = false;
                    LoadTheme(cDrpTheme.SelectedValue);
                    break;
            }

        }
    }
}